// @ts-nocheck
import prompt from '@system.prompt';
import router from '@system.router';

export default {
    data: {
        optionActive: false,
        showBackBt: false,
        goodsList: [],
        fresh:false
    },
    computed: {
        isAllChecked: function () {
            // 计算是否全选
            for (let i = 0; i < this.goodsList.length; i++) {
                if (!this.goodsList[i].checked) {
                    return false
                }
            }
            return this.goodsList.length > 0
        },
        goodsCount: function () {
            // 计算选中的商品总价
            var count = 0
            this.goodsList.forEach(element => {
                if (element.checked) {
                    count += element.num * element.itemPrice
                }
            });
            return count
        },
        goodsNum: function () {
            // 计算选中的商品的数量
            var num = 0
            this.goodsList.forEach(element => {
                if (element.checked) {
                    num += element.num
                }
            });
            return num
        },
    },
    onInit() {
        // 初始化数据
        this.showBackBt = router.getParams() != undefined
        this.getData()
    },
    getData() {
        console.log("trolley page get data")
        this.goodsList = getApp().globalData.cartData
    },
    onShow() {
        this.getData()
    },
    optionActiveChange() {
        // 右上角编辑选项改变
        this.optionActive = !this.optionActive
    },
    allChecked() {
        // 全选
        var allChecked = this.isAllChecked
        console.log((allChecked ? "取消" : "") + "全选")
        for (let i = 0; i < this.goodsList.length; i++) {
            this.goodsList[i].checked = getApp().globalData.cartData[i].checked = !allChecked
        }
    },
    checkedChange(event) {
        let i = event.detail.i
        this.goodsList[i].checked = getApp().globalData.cartData[i].checked = !this.goodsList[i].checked
    },
    numMinusOne(event) {
        var i = event.detail.i
        getApp().globalData.cartData[i].num = (this.goodsList[i].num -= 1)
    },
    numPlusOne(event) {
        var i = event.detail.i
        getApp().globalData.cartData[i].num = (this.goodsList[i].num += 1)
    },
    numSet(event) {
        var i = event.detail.i
        var num = event.detail.num
        this.goodsList[i].num = getApp().globalData.cartData[i].num = num
    },
    settlement() {
        //结算方法
        if (this.goodsNum > 0) {
            router.push({
                uri: "pages/orderCreate/orderCreate",
                params: {
                    goods: this.goodsList.filter(function (goods) {
                        return goods.checked
                    })
                }
            })
        } else {
            prompt.showToast({
                message: "请选择商品"
            })
        }
    },
    deleteSelected() {
        //删除选中
        if (this.goodsNum == 0) {
            prompt.showToast({
                message: "请选择商品"
            })
        } else {
            getApp().globalData.cartData = this.goodsList= this.goodsList.filter(function (item) {
                return!item.checked
            })
            // 后端交互
        }
    },
    goodsOnclick(i) {
        router.push({
            uri: "pages/detail/detail",
            params: {
                commodity: this.goodsList[i]
            }
        })
    },
    back() {
        if (this.showBackBt) {
            router.back()
        }
    },
    refresh(event) {
        this.fresh = event.refreshing

        this.getData()

        this.fresh = false
        prompt.showToast({
            message: '刷新完成!'
        })
    },
}
